home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-11 | 17.7 KB | 594 lines | [TEXT/MPS ] |
- /*
- File: CappuccinoAction.cpp
-
- Contents: Classes for undo/redo support.
-
- Written by: Troy Gaul
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- */
-
- // -- Compiler/Preprocessor Switches --
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- // Exceptions define several important macros (eg. CHECKENV)
- // which are used in the SOM method dispatch glue. If Except.h
- // is not included early enough, exceptions may not be thrown
- // correctly when returning from a SOM method with the "ev" parameter set.
- #include <Except.h>
- #endif
-
- // -- Cappuccino Includes --
-
- #ifndef _CAPPUCCINOACTION_
- #include "CappuccinoAction.h"
- #endif
-
- #ifndef _CAPPUCCINO_
- #include "Cappuccino.h"
- #endif
-
- #ifndef _CAPPUCCINOCONTENT_
- #include "CappuccinoContent.h"
- #endif
-
- #ifndef _CAPPUCCINODEF_
- #include "CappuccinoDef.h"
- #endif
-
- #ifndef _CAPPUCCINOGLOBALS_
- #include "CappuccinoGlobals.h"
- #endif
-
- // -- OpenDoc Includes --
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_ODClipboard_xh
- #include <Clipbd.xh>
- #endif
-
- #ifndef SOM_ODDragItemIterator_xh
- #include <DgItmIt.xh>
- #endif
-
- #ifndef SOM_ODDragAndDrop_xh
- #include <DragDrp.xh>
- #endif
-
- #ifndef SOM_ODUndo_xh
- #include <Undo.xh>
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _FOCUSLIB_
- #include <FocusLib.h>
- #endif
-
- #ifndef _ODDEBUG_
- #include <ODDebug.h>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- //------------------------------------------------------------------------------
- // SCappuccinoActionState
- //------------------------------------------------------------------------------
-
- struct SCappuccinoActionState
- {
- CAction* fAction;
- };
-
- //==============================================================================
- // CAction
- //==============================================================================
- #pragma mark • CAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CAction
- //------------------------------------------------------------------------------
-
- CAction::CAction( Cappuccino* part, ActionCode actionCode )
- {
- fPart = part;
- fSession = ODGetSession(somGetGlobalEnvironment(), fPart->GetODPart());
- fActionCode = actionCode;
- }
-
- //------------------------------------------------------------------------------
- // Method: Destructor
- // Origin: CAction
- //------------------------------------------------------------------------------
-
- CAction::~CAction()
- {
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleDo
- // Origin: CAction
- //
- // Description: This function call the Do method of this class. Generally,
- // you should override that method to provide the actual
- // implementation of the change you want stored in the action.
- //------------------------------------------------------------------------------
-
- void CAction::HandleDo( Environment* ev )
- {
- this->Do(ev);
- this->AddToActionHistory(ev);
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleUndo
- // Origin: CAction
- //
- // Description: This function call the Undo method of this class. Generally,
- // you should override that method to provide the actual
- // implementation of the change you want stored in the action.
- //------------------------------------------------------------------------------
-
- void CAction::HandleUndo( Environment* ev )
- {
- this->Undo(ev);
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleRedo
- // Origin: CAction
- //
- // Description: This function call the Redo method of this class. Generally,
- // you should override that method to provide the actual
- // implementation of the change you want stored in the action.
- //------------------------------------------------------------------------------
-
- void CAction::HandleRedo( Environment* ev )
- {
- this->Redo(ev);
- }
-
- //------------------------------------------------------------------------------
- // Method: GetUndoRedoName
- // Origin: CAction
- //------------------------------------------------------------------------------
-
- const ODBoolean kUndoName = kODTrue;
- const ODBoolean kRedoName = kODFalse;
-
- ODName* CAction::GetUndoRedoName( Environment* ev,
- ODBoolean isUndo )
- {
- ODUShort kindOffset = (isUndo) ? kUndoOffset : kRedoOffset;
-
- // Start withe the prefix.
- Str255 nameStr;
- ODGetIndString(nameStr, kActionStringResID,
- (fActionCode - 1) * kActionCodeMultiplier + kindOffset);
- return CreateITextPString(gGlobals->fEditorsScript, gGlobals->fEditorsLanguage,
- nameStr);
- }
-
- //------------------------------------------------------------------------------
- // Method: AddToActionHistory
- // Origin: CAction
- //------------------------------------------------------------------------------
-
- void CAction::AddToActionHistory( Environment* ev, ODActionType actionType )
- {
- // Get the undo and redo menu item strings.
- TempODName undoName = this->GetUndoRedoName(ev, kUndoName);
- TempODName redoName = this->GetUndoRedoName(ev, kRedoName);
-
- // Stuff our action data into a ByteArray and add it to the action history.
- ODUndo* undo = fSession->GetUndo(ev);
- TempODActionData actionData = this->CreateActionData();
-
- undo->AddActionToHistory(ev, fPart->GetODPart(), actionData,
- actionType, undoName, redoName);
- }
-
- //------------------------------------------------------------------------------
- // Function: CreateActionData
- // Origin: CAction
- //
- // Description: This method is called when this action must be encapsulated
- // into an ODActionData byte array.
- //------------------------------------------------------------------------------
-
- ODActionData* CAction::CreateActionData()
- {
- SCappuccinoActionState state;
- state.fAction = this;
-
- return CreateByteArray(&state, sizeof state);
- }
-
- //------------------------------------------------------------------------------
- // Function: GetActionStateAction [static]
- // Origin: CAction
- //
- // Description: This method is called when the action object referred to by
- // an ODActionData byte array needs to be recovered so that it
- // can be used.
- //------------------------------------------------------------------------------
-
- CAction* CAction::GetActionStateAction(ODActionData* actionState)
- {
- return ((SCappuccinoActionState*) actionState->_buffer)->fAction;
- }
-
- //==============================================================================
- // CTextChangeAction
- //==============================================================================
- #pragma mark • CTextChangeAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CTextChangeAction
- //------------------------------------------------------------------------------
-
- CTextChangeAction::CTextChangeAction( Cappuccino* part,
- ActionCode actionCode,
- CCappuccinoContent* newContent)
- : CAction(part, actionCode)
- {
- fCurrentContent = kODNULL;
- fPreviousContent = kODNULL;
-
- if (newContent)
- newContent->Acquire();
- fCurrentContent = newContent;
- }
-
- //------------------------------------------------------------------------------
- // Method: Destructor
- // Origin: CTextChangeAction
- //------------------------------------------------------------------------------
-
- CTextChangeAction::~CTextChangeAction()
- {
- if (fPreviousContent)
- fPreviousContent->Release();
- if (fCurrentContent)
- fCurrentContent->Release();
- }
-
- //------------------------------------------------------------------------------
- // Method: Do
- // Origin: CTextChangeAction
- //------------------------------------------------------------------------------
-
- void CTextChangeAction::Do( Environment* ev )
- {
- // Get the previous content at the time the action is performed.
- fPreviousContent = fPart->GetContent();
- if (fPreviousContent)
- fPreviousContent->Acquire();
-
- fPart->SetContent(ev, fCurrentContent);
- }
-
- //------------------------------------------------------------------------------
- // Method: Undo
- // Origin: CTextChangeAction
- //------------------------------------------------------------------------------
-
- void CTextChangeAction::Undo( Environment* ev )
- {
- fPart->SetContent(ev, fPreviousContent);
- }
-
- //------------------------------------------------------------------------------
- // Method: Redo
- // Origin: CTextChangeAction
- //------------------------------------------------------------------------------
-
- void CTextChangeAction::Redo( Environment* ev )
- {
- fPart->SetContent(ev, fCurrentContent);
- }
-
- //==============================================================================
- // CClipboardTextChangeAction
- //==============================================================================
- #pragma mark • CClipboardTextChangeAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CClipboardTextChangeAction
- //------------------------------------------------------------------------------
-
- CClipboardTextChangeAction::CClipboardTextChangeAction( Cappuccino* part,
- ActionCode actionCode,
- ODCloneKind cloneKind,
- CCappuccinoContent* content)
- : CTextChangeAction(part, actionCode, content)
- {
- fCloneKind = cloneKind;
- fUpdateID = kODUnknownUpdate;
- }
-
- //------------------------------------------------------------------------------
- // Method: Destructor
- // Origin: CClipboardTextChangeAction
- //------------------------------------------------------------------------------
-
- CClipboardTextChangeAction::~CClipboardTextChangeAction()
- {
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleDo
- // Origin: CClipboardTextChangeAction
- //------------------------------------------------------------------------------
-
- void CClipboardTextChangeAction::HandleDo( Environment* ev )
- {
- fUpdateID = fSession->GetClipboard(ev)->ActionDone(ev, fCloneKind);
-
- Inherited::HandleDo(ev);
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleUndo
- // Origin: CClipboardTextChangeAction
- //------------------------------------------------------------------------------
-
- void CClipboardTextChangeAction::HandleUndo( Environment* ev )
- {
- Inherited::HandleUndo(ev);
-
- // Let the clipboard know.
- WASSERT(fUpdateID != kODUnknownUpdate);
- fSession->GetClipboard(ev)->ActionUndone(ev, fUpdateID, fCloneKind);
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleRedo
- // Origin: CClipboardTextChangeAction
- //------------------------------------------------------------------------------
-
- void CClipboardTextChangeAction::HandleRedo( Environment* ev )
- {
- Inherited::HandleRedo(ev);
-
- // Let the clipboard know.
- WASSERT(fUpdateID != kODUnknownUpdate);
- fSession->GetClipboard(ev)->ActionRedone(ev, fUpdateID, fCloneKind);
- }
-
- //==============================================================================
- // CCutAction
- //==============================================================================
- #pragma mark • CCutAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CCutAction
- //------------------------------------------------------------------------------
-
- CCutAction::CCutAction( Cappuccino* part )
- : CClipboardTextChangeAction(part, kActionCut, kODCloneCut, kODNULL)
- {
- Environment* ev = somGetGlobalEnvironment();
-
- fCurrentContent = new CCappuccinoContent(part);
- fCurrentContent->InitCappuccinoContent(ev);
- }
-
- //==============================================================================
- // CPasteAction
- //==============================================================================
- #pragma mark • CPasteAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CPasteAction
- //------------------------------------------------------------------------------
-
- CPasteAction::CPasteAction( Cappuccino* part, CCappuccinoContent* content )
- : CClipboardTextChangeAction(part, kActionPaste, kODClonePaste, content)
- {
- }
-
- //==============================================================================
- // CClearAction
- //==============================================================================
- #pragma mark • CClearAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CClearAction
- //------------------------------------------------------------------------------
-
- CClearAction::CClearAction( Cappuccino* part )
- : CTextChangeAction(part, kActionClear, kODNULL)
- {
- Environment* ev = somGetGlobalEnvironment();
-
- fCurrentContent = new CCappuccinoContent(part);
- fCurrentContent->InitCappuccinoContent(ev);
- }
-
- //==============================================================================
- // CDropAction
- //==============================================================================
- #pragma mark • CDropAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CDropAction
- //------------------------------------------------------------------------------
-
- CDropAction::CDropAction( Cappuccino* part, CCappuccinoContent* content )
- : CTextChangeAction(part, kActionDrop, content)
- {
- }
-
- //==============================================================================
- // CDragBeginAction
- //==============================================================================
- #pragma mark • CDragBeginAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CDragBeginAction
- //------------------------------------------------------------------------------
-
- CDragBeginAction::CDragBeginAction( Cappuccino* part )
- : CAction(part, kActionDrag)
- {
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleDo
- // Origin: CDragBeginAction
- //------------------------------------------------------------------------------
-
- void CDragBeginAction::HandleDo( Environment* ev )
- {
- this->AddToActionHistory(ev, kODBeginAction);
- // Don't call Inherited as we're replacing its functionality.
- }
-
- //==============================================================================
- // CDragEndAction
- //==============================================================================
- #pragma mark • CDragEndAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CDragEndAction
- //------------------------------------------------------------------------------
-
- CDragEndAction::CDragEndAction( Cappuccino* part )
- : CAction(part, kActionDrag)
- {
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleDo
- // Origin: CDragEndAction
- //------------------------------------------------------------------------------
-
- void CDragEndAction::HandleDo( Environment* ev )
- {
- this->AddToActionHistory(ev, kODEndAction);
- // Don't call Inherited as we're replacing its functionality.
- }
-
- //==============================================================================
- // CSetTextAction
- //==============================================================================
- #pragma mark • CSetTextAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CSetTextAction
- //------------------------------------------------------------------------------
-
- CSetTextAction::CSetTextAction( Cappuccino* part, CCappuccinoContent* content )
- : CTextChangeAction(part, kActionSetText, content)
- {
- }
-
- //==============================================================================
- // CSettingsChangeAction
- //==============================================================================
- #pragma mark • CSettingsChangeAction •
-
- //------------------------------------------------------------------------------
- // Method: Constructor
- // Origin: CSettingsChangeAction
- //------------------------------------------------------------------------------
-
- CSettingsChangeAction::CSettingsChangeAction( Cappuccino* part,
- CSettings* newSettings)
- : CAction(part, kActionChangeSettings)
- {
- fCurrentSettings = kODNULL;
- fPreviousSettings = kODNULL;
-
- if (newSettings)
- newSettings->Acquire();
- fCurrentSettings = newSettings;
- }
-
- //------------------------------------------------------------------------------
- // Method: Destructor
- // Origin: CSettingsChangeAction
- //------------------------------------------------------------------------------
-
- CSettingsChangeAction::~CSettingsChangeAction()
- {
- if (fPreviousSettings)
- fPreviousSettings->Release();
- if (fCurrentSettings)
- fCurrentSettings->Release();
- }
-
- //------------------------------------------------------------------------------
- // Method: Do
- // Origin: CSettingsChangeAction
- //------------------------------------------------------------------------------
-
- void CSettingsChangeAction::Do( Environment* ev )
- {
- // Get the previous content at the time the action is performed.
- fPreviousSettings = fPart->GetSettings();
- if (fPreviousSettings)
- fPreviousSettings->Acquire();
-
- fPart->SetSettings(ev, fCurrentSettings);
- }
-
- //------------------------------------------------------------------------------
- // Method: Undo
- // Origin: CSettingsChangeAction
- //------------------------------------------------------------------------------
-
- void CSettingsChangeAction::Undo( Environment* ev )
- {
- fPart->SetSettings(ev, fPreviousSettings);
- }
-
- //------------------------------------------------------------------------------
- // Method: Redo
- // Origin: CSettingsChangeAction
- //------------------------------------------------------------------------------
-
- void CSettingsChangeAction::Redo( Environment* ev )
- {
- fPart->SetSettings(ev, fCurrentSettings);
- }
-
-